package com.ken.common.mysql.aop;

import com.ken.common.mysql.page.KenPage;
import com.ken.common.mysql.page.Page;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class PagingAop {

    /**
     * 环绕增强所有添加了@Paging注解的方法
     * @return
     */
    @Around("@annotation(com.ken.common.mysql.annotation.Paging)")
    public Object pagingAop(ProceedingJoinPoint joinPoint) throws Throwable {

        //开启分页
        Page page = KenPage.getPage();
        //判断当前是否为自动分页
        boolean isAutoPage = page.isEnable();
        if (page != null && !isAutoPage) {
            //开启分页
            page.setEnable(true);
        }

        try {
            //放行
            return joinPoint.proceed();
        } catch (Throwable throwable) {
            throw throwable;
        } finally {
            if (page != null && !isAutoPage) {
                //关闭分页
                page.setEnable(false);
            }
        }

    }
}
